/*
 * Adam Blackwell
 * ada2358@ccs.neu.edu
 */

/***********************************************************************
 * Clock class
 * 
 * Implements a clock that notifies its observers at every tick
 *
 * Signature:
 *  public static Clock create (int initTime);
 *  public void registerObserver (Observer<Integer> obs);
 *  public void tick ();
 ***********************************************************************/

/**
 * Clock:
 * @author Adam
 */
public class Clock {
  private List<Observer<Integer>> observers;
  private int                     time;
  
  /**
   * Protected Constructor? Why?:
   * @param c
   */
  protected Clock(int c) {
    observers = List.empty();
  }
  
  /**
   * Creator:
   * @param c
   * @return
   */
  public static Clock create(int c) {
    return new Clock(c);
  }
  
  /**
   * Time:
   * @author Adam
   * @return
   */
  public int time() {
    return time;
  }
  
  /**
   * RegesterObserver:
   * Adds an artifact to a list to be notified on clock tick.
   * @author Adam
   * @param ob
   */
  public void registerObserver(Observer<Integer> ob) {
    observers = List.cons(ob, observers);
  }
  
  /**
   * Tick:
   * Pretty self explanatory.
   * @author Adam
   */
  public void tick() {
    // *Tick*:
    time = (this.time + 1);
    // Notify:
    FuncIterator<Observer<Integer>> it = observers.getFuncIterator();
    while (it.hasElement()) {
      it.current().notify(time);
      it = it.advance();
    }
  }
}
